package com.kenyi.co.ui.me.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.common.ui.BaseActivity;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.okhttp.utils.GsonResultok;
import com.kenyi.co.utils.DataCleanManager;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.SharepUtils;
import com.kenyi.co.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

import static com.kenyi.co.utils.CommonUtils.getAppVersion;


/**
 * 设置页面
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SettingActivity";
    private TextView tv_home_title, tv_version;
    private Toolbar toolbar;
    private RelativeLayout re_me_setting_exit, re_me_setting_version, re_me_setting_huancun;
    private TextView tv_huancun;
    private ImageView iv_me_huancun_go;
    private Boolean islogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null && bundle.containsKey("islogin")) {
            islogin = bundle.getBoolean("islogin");
        }
        initView();
        String ss;
        try {
            ss = DataCleanManager.getTotalCacheSize(SettingActivity.this);
            tv_huancun.setText(ss);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.white));//设置状态栏颜色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        }
    }

    private void initView() {
        setStatusBar();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
        tv_home_title = (TextView) findViewById(R.id.tv_home_title);

        tv_home_title.setText("系统设置");

        re_me_setting_exit = findViewById(R.id.re_me_setting_exit);
        re_me_setting_version = findViewById(R.id.re_me_setting_version);
        re_me_setting_huancun = findViewById(R.id.re_me_setting_huancun);
        iv_me_huancun_go = findViewById(R.id.iv_me_huancun_go);
        tv_version = findViewById(R.id.tv_version);
        tv_huancun = findViewById(R.id.tv_huancun);
        re_me_setting_version.setOnClickListener(this);
        re_me_setting_huancun.setOnClickListener(this);
        re_me_setting_exit.setOnClickListener(this);
        tv_version.setText(getAppVersion(this));
        if (!islogin) {
            re_me_setting_exit.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        MobclickAgent.onPageStart("设置页面");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("设置页面");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.re_me_setting_exit:
                Logout();
                break;
            case R.id.re_me_setting_huancun:
                DataCleanManager.clearAllCache(this);
                String ss;
                try {
                    ss = DataCleanManager.getTotalCacheSize(SettingActivity.this);
                    tv_huancun.setText(ss);
                    if (ss.equals("0M")) {
                        iv_me_huancun_go.setVisibility(View.INVISIBLE);
                    } else {
                        iv_me_huancun_go.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.re_me_setting_version:
                ToastUtil.showShort(getApplicationContext(), "当前版本");
                break;
        }
    }

    public void Logout() {


        Map<String, String> params = new HashMap<>();

        params.put("userId", SharepUtils.getString_info(SettingActivity.this, SharepUtils.USER_LOGIN_USERID));
        Map<String, String> headers = new HashMap<>();
        headers.put("APP-Content-Type", "application/json");
        headers.put("Authorization", SharepUtils.getString_info(SettingActivity.this, SharepUtils.USER_LOGIN_TOKEN));

        String url = NetConfig.AUTH_LOGOUT;
        OkHttpUtils.post()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {


                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();

                        int num = 0;
                        GsonResultok packlist = gson.fromJson(response, GsonResultok.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            List<String> LikeTags1;
                            try {
                                LikeTags1 = SharepUtils.getShDataList(SettingActivity.this, SharepUtils.USER_FAV_TAGS_lOCAL, String.class);
                                SharepUtils.setShDataList(SettingActivity.this, SharepUtils.USER_FAV_TAGS_LIST, LikeTags1);
                            } catch (Exception e) {

                            }
                            SharepUtils.setString_info(SettingActivity.this, "0", SharepUtils.USER_IS_LOGIN);
                            SharepUtils.deleteUserInfo(SettingActivity.this);
                            ToastUtil.showShort(getApplicationContext(), packlist.getMsg());
                            SharepUtils.setString_info(SettingActivity.this, "1", SharepUtils.USER_LOGIN_ONE);
                            finish();
                        } else {

                        }

                    }
                });
    }
}
